package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import weibo4j.Weibo;
import weibo4j.model.Annotation;
import weibo4j.model.Places;
import weibo4j.model.Source;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

public class PlaceCheckinNumSZ extends Weibo {
	/**
	 * 程序主要是按照半径抓取附近一段时间的签到用户和poi。 1.主要是更改起止时间 2.搜索半径为2000米
	 * 3.假如程序中途崩掉，要继续运行则要看，抓到了哪个网格了，还有就是token蹦掉的时间。如果超过两个小时则为0，如果没有就写下一个。
	 */
	private static final long serialVersionUID = 1L;
	private String access_token[] = new String[33];
	private DBConn db = new DBConn();
	private int pageLimit = 50;
	private int dayLimit = 1; // change the days, daylimit+id=32

	public PlaceCheckinNumSZ() {
		access_token[0] = "2.00i_gE4CMLlPQDf42a44c1250fP99_";
		access_token[1] = "2.00i_gE4CTosKBE69d0127ea44fXcED";
		access_token[2] = "2.00i_gE4CmpTy_C8a3cddf65e45u4xD";
		access_token[3] = "2.00i_gE4CDdrAcC8e32dbf1bdW13XKE";
		access_token[4] = "2.00i_gE4C05c3VQ6f14ce7e1bR9EjTD";
		access_token[5] = "2.00i_gE4CF5GmrDb3ed1ad09eCHw_vD";
		access_token[6] = "2.00i_gE4Cwzl7MC92d2f4d7c2GFa23D";
		access_token[7] = "2.00i_gE4CWh1liB22cf8784d30T5R25";
		access_token[8] = "2.00i_gE4C0g1SIObaa6dc5ccb0Hn5n7";
		access_token[9] = "2.00i_gE4C6mzl9D019603e1ccUdpG_E";
		access_token[10] = "2.00Uu17fDMjsccBdc9ab6ae6c0_luH1";
		access_token[11] = "2.00Uu17fD2lkv2D5492d1e69dmwDbvC";
		access_token[12] = "2.00Uu17fD4yAhECdff0068244o34jCE";
		access_token[13] = "2.00Uu17fDa7JFPE31daa76b74arsGbD";
		access_token[14] = "2.00Uu17fDIsh3xC24739a2e640a8id9";
		access_token[15] = "2.00Uu17fDTHsKTD9d0693fda0q_ztZE";
		access_token[16] = "2.00Uu17fDCqn3MEa1ad0e70f6bkq4QB";
		access_token[17] = "2.00Me2rVDaCgTcEe1089fe19dRNO7kD";
		access_token[18] = "2.00Me2rVD7PEMhDf1ffe7336dFoFU4C";
		access_token[19] = "2.00Me2rVDBcro6E81c93eff01JmIsOB";
		access_token[20] = "2.00Me2rVDT7KHJD88e1655feefTzkKE";
		access_token[21] = "2.00YFodoFBSiemD77dee85a12xDt88B";
		access_token[22] = "2.00YFodoF0lcctOacaecd60a8x8FRWC";
		access_token[23] = "2.00joo_tCQicNqC5718b2e5fbT_jnSC";
		access_token[24] = "2.00joo_tC7MBPNE2ef3ca320d66JtVC";
		access_token[25] = "2.00joo_tCm2VVACa99fce513bBvGteD";
		access_token[26] = "2.00joo_tCpUeR9E5e34fc88b10lbPeT";
		access_token[27] = "2.00joo_tCxR7HyD043ba7228euXuo2C";
		access_token[28] = "2.00joo_tCvY9SdBf60f0e366endRsaD";
		access_token[29] = "2.00joo_tCtRbb5B9b889b929dYVMQDD";
		access_token[30] = "2.00joo_tCgHBjHB6820f00ee3c88aZD";
		access_token[31] = "2.00joo_tCbxytIDd1ef6bc6e43HIhPE";
		access_token[32] = "2.00joo_tC0YG1kEb955cccfe20use64";
		//access_token[21] = "2.00s7jH_D62zzWD587662c127r5WnWB";
		//access_token[22] = "2.00s7jH_DrjRaVB83920a5138RQLYFB";
		//access_token[23] = "2.00s7jH_DYI9RgD2e6b0ba071bcRLMB";
     	//access_token[24] = "2.00s7jH_D6f_XJD5b7e69f854ql9iwB";
		//access_token[25] = "2.00s7jH_DdOvccB9089cf4605vmHs2E";
		//access_token[28] = "2.00s7jH_D0DjAhi6535c252510y2CnJ";
		//access_token[29] = "2.00s7jH_D0r9Uin661985f8cfoKYCTC";
		//access_token[30] = "2.00s7jH_DUgf4cBe8b928bd2e5r9fyD";
		int idloop = 0;// 计算一个token的查询次数
		int tokenloop =20;// token 失效的时候使用
		weibo4j.Place wp = new weibo4j.Place();
		wp.client.setToken(access_token[tokenloop]); // change the idloop
		long starttime = System.currentTimeMillis();// 一轮token抓取的开始时间
		// 存储网格id文件
		File filename = new File("d://gridId1.txt");
		try {
			InputStreamReader reader = new InputStreamReader( new FileInputStream(filename), "UTF-8");
			BufferedReader br = new BufferedReader(reader);
			String sGridId = br.readLine(); 
			while (sGridId != null && !"".equals(sGridId)) {
				int gridId = Integer.parseInt(sGridId);
				// 解析网格id以获取其经纬度
				double sPos[] = db.stationPosition(gridId);
				int[] start = new int[dayLimit];int [] end = new int[dayLimit];
				if (sPos[0] != 0.0 && sPos[1] != 0.0) {
					int id = 38; // for update the bjsubway table【没有用到】
					int totalPage = 0;
					int range = 2000;// 搜索半径2000米
					long[] totalwb = new long[dayLimit];
					// 更改时间戳
					//开始抓取2014年的数据了
					start[0] = 1293811200;// 20120120//change the start
											//  time
					end[0] =1325347200   ;// start[0]+86400; //12 hours, from
							// 9:00~21:00
					// wp.client.setToken(access_token[tokenloop]);
					// tokenloop=tokenloop+1;
					// if(tokenloop==21)
					// tokenloop=0;
					for (int i = 1; i <= dayLimit; i++) { // 150 days
						int page = 1;
						do {
							// 一个token抓135次然更新token
							if (idloop > 135) {
								if (tokenloop < 32) {
									// Thread.sleep(10000);
									tokenloop = tokenloop + 1;
									wp.client.setToken(access_token[tokenloop]);
									idloop = 0;
								} else {
									// 一轮token抓取花费的时间
									long costtime = System.currentTimeMillis() - starttime;
									// 一轮抓完与下一轮开始要休息至少休息1个小时的时间
									if (costtime < 3600000) {
										System.out.println("wait...");
										Thread.sleep(7200000 - costtime);
									}
									tokenloop = 5;
									starttime = System.currentTimeMillis();
									idloop = 0;
									wp.client.setToken(access_token[tokenloop]);
									// page = totalPage+1;
								}
							}
							idloop = idloop + 1;
							// 经纬度，半径，起止时间
							// 和排序方式。默认为0，按时间排序；为1时按与中心点距离进行排序，单页返回的记录条数，最大为50，返回结果的页码
							StatusWapper sw = wp.getNearbyTimeline(Double.valueOf(sPos[0]), Double.valueOf(sPos[1]),
									Integer.valueOf(range), Integer.valueOf(start[i - 1]), Integer.valueOf(end[i - 1]),
									Integer.valueOf(1), Integer.valueOf(50), Integer.valueOf(page));
							System.out.println("current station: " + sGridId + ", tokenloop = " + tokenloop);
							if (sw != null) {
								if (page == 1) {// 假如是第一页，就要计算总的页码
									totalwb[i - 1] = sw.getTotalNumber();
									System.out.println("totalwb: " + totalwb[i - 1]);
									totalPage = (int) (totalwb[i - 1] / 50 + 1);
									if (totalPage > pageLimit)// 限制了一个网格最多抓50页
										totalPage = pageLimit;
								}
								for (Status st : sw.getStatuses()) {
									if (st != null) {
										User user = st.getUser();
										if (user != null) {
											String useridtmp = user.getId();
											if (db.queryUserId(useridtmp) == null) {// 如果数据库中没有则插入用户数据
												System.out.println("Insert user: " + useridtmp);
												// Thread.sleep(1000);
												insertUser(db, user);
												String poiid = insertStatus(db, st, useridtmp, gridId);
												if (poiid != null && !"".equals(poiid) && !"null".equals(poiid)) {
													if (db.queryPoiId(poiid) == null) {// 如果数据库中没有则插入poiid数据
														// 每次用到wp就需要检查token失效没
														idloop = idloop + 1;
														if (idloop > 135) {
															if (tokenloop < 32) {
																// Thread.sleep(10000);
																tokenloop = tokenloop + 1;
																wp.client.setToken(access_token[tokenloop]);
																idloop = 0;
															} else {
																long costtime = System.currentTimeMillis() - starttime;
																if (costtime < 3600000) {
																	System.out.println("wait...");
																	Thread.sleep(7200000 - costtime);
																}
																tokenloop = 0;
																starttime = System.currentTimeMillis();
																idloop = 0;
																wp.client.setToken(access_token[tokenloop]);
																// page =
																// totalPage+1;
															}
														}
														Places pl = wp.poisShow(poiid);
														if (pl.getPoiid() != null && !"".equals(pl.getPoiid())
																&& !"null".equals(pl.getPoiid())) {
															insertPois(pl);
														}
													}
												}
											}
										}
									}
								}
								page = page + 1; // for the daily totalwb record
							} else {
								page = 1000;
							}
						} while (page <= totalPage);
						// insertStatusByStation(id,start[i-1],sName,
						// totalwb[i-1]);
						id = id + 1; // add one record for each station in each
										// day
						if (i < dayLimit) {// 由于dayLimit为1所以没有用到
							start[i] = start[i - 1] + 86400;
							end[i] = end[i - 1] + 86400;
						}
					}
					// System.out.println("station[" + id + "]: " + sName + ",
					// idloop = " + idloop + ", tokenloop = " + tokenloop);
				}
				sGridId = br.readLine();
				// Thread.sleep(660000);
			}
			System.out.println("Stop at station: " + sGridId + ", tokenloop = " + tokenloop);
			br.close();
			db.close();
		} catch (WeiboException e) {
			db.close();
			e.printStackTrace();
		} catch (IOException e) {
			db.close();
			e.printStackTrace();
		} catch (SQLException e) {
			db.close();
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// System.out.println(Arrays.toString(totalwb));
		// System.out.println(start[119]);
		// System.out.println(System.currentTimeMillis()/1000);
		// try {
		// wp.poisUsersList(args[0]);
		// wp.userInfoInLBS(args[0]);
		// } catch (WeiboException e) {
		// e.printStackTrace();
		// }
	}

	public void insertUser(DBConn db, User u) {

		Date dt = u.getCreatedAt();
		if (dt != null) {// 防止此用户不存在的情形
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String createAt = sdf.format(dt);

			String regexp = "\'";
			String sname = (u.getScreenName()).replaceAll(regexp, " ");
			String uname = (u.getName()).replaceAll(regexp, " ");
			String disp = (u.getDescription()).replaceAll(regexp, " ");

			db.insertUser(u.getId(), sname, uname, u.getProvince(), u.getCity(), u.getLocation(), disp,
					u.getUserDomain(), u.getGender(), u.getFollowersCount(), u.getFriendsCount(), u.getStatusesCount(),
					u.getFavouritesCount(), createAt, u.isAllowGeo());
		}
	}

	/**
	 * 没有插入poiid 到数据库中，只是获取状态中的poiid
	 * 
	 * @param db
	 * @param st
	 * @param uid
	 * @param gridId
	 * @return
	 */
	public String insertStatus(DBConn db, Status st, String uid, int gridId) {// add
																				// station
																				// name
		String poiid = "";
		if (st != null) {
			String stext = st.getText();
			boolean sDeleted = stext.contains("抱歉，此微博已被作者删除。查看帮助");
			boolean sRepost = stext.contains("转发微博");
			boolean sConsult = stext.contains("此微博已被删除");
			if (!sDeleted && !sRepost && !sConsult) {// &&!sRepost除去转发和删除的微博

				String regexp = "\'";
				stext = stext.replaceAll(regexp, " ");

				Date dt = st.getCreatedAt();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String createAt = sdf.format(dt);

				double lon = 0.0;
				double lat = 0.0;
				String geo = st.getGeo();
				if (geo != null && !"".equals(geo) && !"null".equals(geo)) {
					lon = st.getLongitude();
					lat = st.getLatitude();
				}

				String annot = st.getAnnotations();
				annot = annot.replaceAll(regexp, " ");
				if (annot != null) {
					System.out.println(annot);
					Annotation anno = st.getAnno();
					if (anno != null && !"".equals(anno) && !"null".equals(anno)) {
						weibo4j.model.Place place = anno.getPlace();
						if (place != null && !"".equals(place) && !"null".equals(place)) {
							lon = place.getLongitude();
							lat = place.getLatitude();
							poiid = place.getPoi();
						} else {
							poiid = "";
						}
					}
				}

				String source = "";
				if (st.getSource() != null) {
					Source sour = st.getSource();
					source = sour.toString();
					source = source.replaceAll(regexp, " ");
				}
				// if(poiid!=null &&!"null".equals(poiid)){
				// db.insertStatusPoi(uid, createAt, st.getId(), st.getMid(),
				// gridId, stext, source,poiid,
				// geo, lon, lat,
				// st.getRepostsCount(),st.getCommentsCount(),annot);
				// }
			}
		}
		return poiid;
	}

	private void insertPois(Places pl) {
		String regexp = "\'";
		String sTitle = pl.getTitle();
		String newTitle = sTitle.replaceAll(regexp, " ");
		String addr = pl.getpoiStreetAdds();
		String newAdd = addr.replaceAll(regexp, addr);
		String addr2 = pl.getpoiStreetAdds();
		String newAdd2 = addr2.replaceAll(regexp, addr2);
		db.insertPois(pl.getPoiid(), newTitle, newAdd, pl.getLon(), pl.getLat(), pl.getCategory(), pl.getCity(),
				pl.getProvince(), pl.getCountry(), pl.getCategorys(), pl.getCategoryName(), pl.getCheckinNum(),
				pl.getCheckinUserNum(), pl.getTipNum(), pl.getPhotoNum(), newAdd2);
	}

	public static void main(String[] args) {
		PlaceCheckinNumSZ dc = new PlaceCheckinNumSZ();
	}

}
